﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace Project_1_refactored_
{
    //Enemy states
    enum enemyStates
    {
        Active,
        inActive,
        Dying
    }

    partial class Enemy : GameObject
    {
        #region Declarations 
        const string ENEMY_idle = "";
        const string ENEMY_run = @"Character\Zombie55x89\Zombie_run";
        const string ENEMY_jump = "";
        const string ENEMY_attack = "";
        const string ENEMY_die = @"Character\Zombie55x89\grave";


        const int ENEMY_SPEED = 160;

        Vector2 eDirection = new Vector2(Move_Left, 0);
        bool bFacesLeft = true;

        const float Move_Left = -1.3f;
        const float Move_Right = 1.3f;

        //to control enemy state
        enemyStates enemyState = enemyStates.inActive;
        public enemyStates eSTATE { get { return enemyState; } }

        #endregion

        #region LoadContent, Update, Draw methods
        public void LoadContent(ContentManager Content)
        {
            Animated = true;
            frameWidth = 96;
            v2Position.X += 500;
            enemyState = enemyStates.Active;
            base.LoadContent(Content, ENEMY_idle, ENEMY_run, ENEMY_jump, ENEMY_attack, ENEMY_die, new int[] { 1, 8, 1, 1, 1 });
            v2Position.Y -= t2dRun.Height; //makes them at the bottom of the screen
            frameHeight = t2dRun.Height; // frame height assignment

            //CHARACTERISTICS INITIALIZATION
            base.newZombie();
            base.LoadHPContent(Content); //HP MINI BAR
        }

        public void Update(GameTime theGameTime)
        {
            switch (enemyState)
            {
                case enemyStates.Active:
                    UpdateMovement();
                    base.HPInfoUpdate(v2Position); // HP MINI BAR UPDATE
                    base.Update(theGameTime, "Run");
                    break;
                case enemyStates.Dying:
                    base.dying = true;
                    base.frameHeight = t2dDie.Height;
                    base.frameWidth = t2dDie.Width;
                    base.Update(theGameTime, "Die");
                    break;
            }
        }

        private void UpdateMovement()
        {
            v2Position += eDirection;
            collisionHandler();
        }

        public new void Draw(SpriteBatch spriteBatch)
        {
            SpriteEffects effect = SpriteEffects.None;
            if(bFacesLeft == false)
                effect = SpriteEffects.FlipHorizontally;

            switch (enemyState)
            {
                case enemyStates.Active:
                    base.DrawHPandTEXT(spriteBatch); //HP MINI BAR DRAW
                    spriteBatch.Draw(t2dRun, new Rectangle((int)v2Position.X, (int)v2Position.Y, frameWidth, frameHeight), getSourceRect(), Color.White, 0f, Vector2.Zero, effect, 0f);
                    break;
                case enemyStates.Dying:  // && !DyingAnimationFinished
                    spriteBatch.Draw(t2dDie, new Rectangle((int)v2Position.X, (int)v2Position.Y, frameWidth, frameHeight), Color.White);
                    break;
            }
        }
        #endregion

        #region public methods
        public void deActivate()
        {
            enemyState = enemyStates.Dying;
            //enemyState = enemyStates.Dying;
        }

        public bool Active()
        {
            bool active = false;

            if (enemyState == enemyStates.Active)
                active = true;
            if (enemyState == enemyStates.inActive)
                active = false;

            return active;
        }

        public void increaseLocation(int X, int Y)
        {
            v2Position.X += X;
            v2Position.Y -= Y;
        }

        //very easy collision handler
        public void collisionHandler()
        {
            if (v2Position.X < 0)
            {
                eDirection.X = Move_Right;
                bFacesLeft = false;
            }
            if (v2Position.X > worldWidth - frameWidth)
            {
                eDirection.X = Move_Left; 
                bFacesLeft = true;
            }
        }
        #endregion

        #region helper methods
        public void applyRangeDmg()
        {
            checkAlive();
            if (HP >= RANGE_ATTACK)
                HP -= RANGE_ATTACK;
        }

        public void applyMeleeDmg()
        {
            checkAlive();
            if (HP >= MELEE_ATTACK)
                HP -= MELEE_ATTACK;
        }

        public void checkAlive()
        {
            if (HP <= 0)
                enemyState = enemyStates.Dying;
        }
        #endregion
    }
}
